import base64

import ddddocr
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait  # 这个是为显隐式等待提供方法的模块
from selenium.webdriver.support import expected_conditions as EC  # 这个是为显示等待提供定位功能的模块

options = webdriver.ChromeOptions()  # 创建谷歌浏览器的options对象来接收浏览器的相关设置

options.add_argument('--start-maximized')  # 最大化窗口
options.add_argument('--incognito')  # 无痕浏览
options.add_argument('--disable-extensions')  # 禁用浏览器的扩展程序,避免干扰网站的打开
options.add_experimental_option("detach", True)  # 禁止浏览器自动关闭
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('useAutomationExtension', False)

# options.add_argument('--headless')
# options.add_argument('user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"')
# options.add_argument('--disable-gpu')

driver = webdriver.Chrome(options)  # 创建谷歌浏览器驱动对象
ocr = ddddocr.DdddOcr(show_ad=False, beta=True)

driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    'source': '''
    Object.defineProperty(navigator, 'webdriver',{get:() => false})
    '''
})

driver.get('https://captcha1.scrape.center/')

wait = WebDriverWait(driver, 20)  # # 设置等待时间

# titles = driver.find_elements(By.CLASS_NAME, 'm-b-sm')
username_inp, password_inp = wait.until(
    EC.presence_of_all_elements_located((By.CLASS_NAME, 'el-input__inner'))
)

username_inp.send_keys('admin')
password_inp.send_keys('admin')


wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.el-button.el-button--primary'))).click()
print(1)

"""
找到canvas画布中的
缺口背景图(题目)：
geetest_canvas_bg geetest_absolute  style="display:none"
找到滑块图 并且将其隐藏  
<canvas class="geetest_canvas_slice geetest_absolute" width="260" height="160" style="display:none"></canvas>

完整背景图(答案)：
geetest_canvas_fullbg geetest_fade geetest_absolute
将隐藏的答案图设置为显示  style="display: block   
<canvas class="geetest_canvas_fullbg geetest_fade geetest_absolute" height="160" width="260" style="display: block; opacity: 0;"></canvas>

滑块小图(条件)：
geetest_canvas_slice geetest_absolute

滑块处理  *完整图+缺口背景图* 或  滑块图+缺口图
"""
# 滑块图
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_canvas_slice.geetest_absolute')))
print(2)
slice_base64 = \
    driver.execute_script(
        "return document.getElementsByClassName('geetest_canvas_slice')[0].toDataURL('image/png')").split(
        ',')[1]
with open('slice.png', 'wb') as fp:
    fp.write(base64.b64decode(slice_base64))


# 缺口背景图
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_canvas_bg.geetest_absolute')))
print(2)
bg_base64 = \
    driver.execute_script(
        "return document.getElementsByClassName('geetest_canvas_bg')[0].toDataURL('image/png')").split(
        ',')[1]
# with open('bg.png', 'wb') as fp:
#     fp.write(base64.b64decode(bg_base64))

# 完整图
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_canvas_fullbg.geetest_fade.geetest_absolute')))
print(3)
fullbg_base64 = \
    driver.execute_script(
        "return document.getElementsByClassName('geetest_canvas_fullbg')[0].toDataURL('image/png')").split(
        ',')[1]
# with open('bg.png', 'wb') as fp:
#     fp.write(base64.b64decode(fullbg_base64))

slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
res = slide.slide_comparison(base64.b64decode(bg_base64), base64.b64decode(fullbg_base64))  #不接收base64编码的数据所以我们要传入base64解码后的字节数据
distance = res['target'][0] #滑块移动的距离

# 动作链接
distance -= 5

mouse = webdriver.ActionChains(driver)
# geetest_slider_button
button = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_slider_button')))   #找到滑块的按钮
mouse.click_and_hold(button)    #点击并且不放
mouse.pause(0.2)
mouse.move_by_offset(xoffset=distance + 10, yoffset=0)  #移动
mouse.pause(1)
mouse.move_by_offset(xoffset=-10, yoffset=0)
mouse.release() #释放
mouse.perform() #开始执行动作链
input()
driver.quit()